package com.motherchild.user.controller;


import com.motherchild.common.ApiResponse;
import com.motherchild.user.dto.ShippingAddressDTO;
import com.motherchild.user.service.ShippingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addresses")
public class ShippingAddressController {

    @Autowired
    private ShippingAddressService shippingAddressService;

    @GetMapping("/get/{userId}")
    public ApiResponse<List<ShippingAddressDTO>> getAddresses(@PathVariable Long userId) {
        List<ShippingAddressDTO> addresses = shippingAddressService.getAddressesByUserId(userId);
        return ApiResponse.success(addresses);
    }

    @PostMapping("/add/{userId}")
    public ApiResponse<Void> addAddress(@RequestBody ShippingAddressDTO addressDTO,@PathVariable Long userId) {
        addressDTO.setUserId(userId);
        shippingAddressService.addAddress(addressDTO);
        return ApiResponse.success();
    }

    @PutMapping("/update/{id}")
    public ApiResponse<Void> updateAddress(@PathVariable Long id, @RequestBody ShippingAddressDTO addressDTO) {
        addressDTO.setId(id);
        shippingAddressService.updateAddress(addressDTO);
        return ApiResponse.success();
    }

    @DeleteMapping("/delete/{id}")
    public ApiResponse<Void> deleteAddress(@PathVariable Long id) {
        shippingAddressService.deleteAddress(id);
        return ApiResponse.success();
    }

    @GetMapping("/getAddressById/{id}")
    public ApiResponse<ShippingAddressDTO> getAddressById(@PathVariable Long id) {
        ShippingAddressDTO address = shippingAddressService.getAddressById(id);
        return ApiResponse.success(address);
    }
}